/*
Author: Surpass-HC
Source: https://gitee.com/hxchuan000/clpc-1
Mail: surpass-hc@qq.com
*/
#include <parmNorm.h>
#include <crcCalc.h>
#include <flashOpt.h>
#include <hexstr.h>
#include <cplTime.h>
#include <genrand.h>

//================================default normal param begin=============================================
DEV_C51_XDATA_FLAG parmInfoNorStc parmNormIstc;
int parmNormPrint()
{
    logdebugc("printf device normal param------>");
    logdebugc("size:%d", parmNormIstc.phead.psize);
    logdebugc("crc:%d", parmNormIstc.phead.pcrc);
    // logdebugc("sn:%.*s", (int)sizeof(parmNormIstc.sn), parmNormIstc.sn);
    logHexBufStr("parmNormPrint sn", parmNormIstc.sn, sizeof(parmNormIstc.sn));
    logdebugc("addr:%d", parmNormIstc.addr485);
    logdebugc("ver:%s", parmNormIstc.ver);
#if PARM_NORMAL_CHK_FLAG
    logdebugc("chkparam:%d", parmNormIstc.chkparam);
#endif
    return 0;
}
int parmNormReset(int configLevel)
{
    switch (configLevel)
    {
    case PARAM_CFG_LEVEL_0:
    {
        memcpy(parmNormIstc.sn, CLPTC_SLAVE_SN_DEFAULT, sizeof(parmNormIstc.sn));
        parmNormIstc.addr485 = CLPTC_SLAVE_BOARDCAST_ADDR;
    }
    case PARAM_CFG_LEVEL_1:
    {
    }
    case PARAM_CFG_LEVEL_2:
    {
    }
    case PARAM_CFG_LEVEL_3:
    {
    }
    case PARAM_CFG_LEVEL_4:
    {
    }
    case PARAM_CFG_LEVEL_5: // defualt
    {
#if PARM_NORMAL_CHK_FLAG
        parmNormIstc.chkparam = 88;
#endif
    }
    case PARAM_CFG_LEVEL_6:
    {
    }
    case PARAM_CFG_LEVEL_7:
    {
    }
    case PARAM_CFG_LEVEL_8:
    {
    }
    case PARAM_CFG_LEVEL_9:
    {
    }
    default:
    {
        break;
    }
    }
    parmCalcHeadInf(parmNormIstc);
    return 0;
}
void parmNormSave(int flashAddr)
{
    parmCalcHeadInf(parmNormIstc);
    flashOptWrite(flashAddr, &parmNormIstc, sizeof(parmNormIstc));
}
void parmNormRead(int flashAddr)
{
    int tmpflag = 0;
    do
    {
        // read form hard
        // for default log param to printf, new param make to default.
        // parmNormReset(PARAM_CFG_LEVEL_0);
        // mcu flash default value is 0xff. so some param maybe is 0xff.
        tmpflag = flashOptRead(flashAddr, &parmNormIstc, sizeof(parmNormIstc));
        logdebugc("---------read flash[%d]:%d,%d", tmpflag, parmNormIstc.phead.psize, (int)paramGetDataSize(parmNormIstc));
        if (tmpflag <= 0)
        {
            // for file
            logdebugc("flash read error");
            break;
        }
        if (parmNormIstc.phead.psize == 0xffff)
        {
            // flash is erase
            logdebugc("flash is have no data");
            break;
        }
        // mcu code, have boot and app, they need common param, and they need motify param. so not calculate crc, fix crc value replace it!
        if (parmNormIstc.phead.pcrc != PARAM_HEAD_CRC_HEX_FIXED(parmNormIstc))
        {
            logdebugc("flash data crc error");
            break;
        }
        return; // ok
    } while (0);
    parmNormReset(PARAM_CFG_LEVEL_0);
    parmNormSave(flashAddr);
}

void prmGetVerStr()
{
    // config device version!
    int tmpVerCount = 0;
    char DEV_C51_XDATA_FLAG tmpCplTime[16] = "20250425111146";
    getCompileTime(__DATE__, __TIME__, tmpCplTime);
    memset(parmNormIstc.ver, 0, sizeof(parmNormIstc.ver));
    memcpy(parmNormIstc.ver + tmpVerCount, CLPTC_DEVICE_VERSION_HEAD, sizeof(CLPTC_DEVICE_VERSION_HEAD));
    tmpVerCount += sizeof(CLPTC_DEVICE_VERSION_HEAD) - 1;
    memcpy(parmNormIstc.ver + tmpVerCount, CLPTC_DEVICE_VERSION_NUMBER, sizeof(CLPTC_DEVICE_VERSION_NUMBER));
    tmpVerCount += sizeof(CLPTC_DEVICE_VERSION_NUMBER) - 1;
    memcpy(parmNormIstc.ver + tmpVerCount, tmpCplTime, 12);
}
int prmGetSnStr()
{
    int i = 0;
    for (i = 0; i < (int)sizeof(parmNormIstc.sn); i++)
    {
        if (parmNormIstc.sn[i] != 0xff)
        {
            break;
        }
    }
    if (i == (int)sizeof(parmNormIstc.sn))
    {
        logdebugc("sn is ff, gen it!");
        genRandHexBuf(parmNormIstc.sn, sizeof(parmNormIstc.sn));
        return -1;
    }
    if (memcmp(parmNormIstc.sn, CLPTC_SLAVE_SN_DEFAULT, sizeof(parmNormIstc.sn)) == 0)
    {
        logdebugc("sn is default, gen it!");
        genRandHexBuf(parmNormIstc.sn, sizeof(parmNormIstc.sn));
        return -2;
    }
    return 1;
}
void parmNormInit(int flashAdr)
{
    parmNormRead(flashAdr);
    // config flash addr!
    parmNormIstc.flashSvAddr = flashAdr;
    // get version.
    prmGetVerStr();
    // config sn if you need! sn can read cpu uuid! mcu have regiter read.
    // linux, we can read mac?cpu?
    if (prmGetSnStr() < 0)
    {
        parmNormSave(flashAdr);
    }
    // do you code! if value is 0xff

    parmNormPrint();
}
//================================default normal param end=============================================
